package com.bhs.sansong.ui.order;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.CompanyInformation;
import com.bhs.sansong.bean.DedicatedLineBean;
import com.bhs.sansong.bean.DedicatedLineDataBean;
import com.bhs.sansong.bean.MenuBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.BottomSelectListDialog;
import com.bhs.sansong.ui.dialog.MyClickListener2;
import com.bhs.sansong.ui.discountCoupon.CouponReceivedFragment;
import com.bhs.sansong.ui.shipments.IssueNewOrderActivity;
import com.bhs.sansong.utils.StatusBarUtil;
import com.bhs.sansong.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 发货取货
 */
public class DispatchAndCollectActivity extends BaseActivity implements View.OnClickListener, NetCallBack {


    private EditText mEtSearch;
    private int logistics_id;

    @Override
    public int initLayout() {
        return R.layout.activity_dispatch_and_collect;
    }


    @Override
    public void initView() {
        logistics_id = getIntent().getIntExtra("logistics_id", 0);
        StatusBarUtil.setTranslucentStatus(this, true);
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }
        findViewById(R.id.rl_shipments).setOnClickListener(this);
        findViewById(R.id.rl_claim_goods).setOnClickListener(this);
        mEtSearch = findViewById(R.id.et_search);
    }

    @Override
    public void initData() {
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索要做的操作
                    startActivity(new Intent(mActivity, ClaimGoodsActivity.class)
                            .putExtra("value", mEtSearch.getText().toString()));
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rl_shipments) {
            networkRequest(netApi.getSpecialList(0, 20, logistics_id), DispatchAndCollectActivity.this, 88);
        } else if (view.getId() == R.id.rl_claim_goods) {
            startActivity(new Intent(mActivity, ClaimGoodsActivity.class)
                    .putExtra("value",""));
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        DedicatedLineDataBean bean = new Gson().fromJson(json, DedicatedLineDataBean.class);
        if (bean.isStatus()) {
            selectLine(bean.getData().getList());
        }else {
            ToastUtil.show(bean.getMsg());
        }
    }

    private void selectLine(List<DedicatedLineBean> list) {
        List<MenuBean> menuBeans = new ArrayList<>();
        for (DedicatedLineBean dedicatedLineBean : list) {
            String line = dedicatedLineBean.getStart_city() + " - " + dedicatedLineBean.getEnd_city();
            line = line.replace("市", "");
            menuBeans.add(new MenuBean(line));
        }

        BottomSelectListDialog listDialog = new BottomSelectListDialog(mActivity, menuBeans);
        listDialog.show();
        listDialog.setMyClickListener(new MyClickListener2() {
            @Override
            public void onClick(String id, String info) {
                DedicatedLineBean data = list.get(Integer.parseInt(info));
                CompanyInformation companyInfo = new CompanyInformation(data.getCompany_pic(), data.getAddress(),
                        String.valueOf(data.getLogistics_id()), data.getLine_id(), data.getCompany_name());
                companyInfo.setStartCity(data.getStart_city());
                companyInfo.setEndCity(data.getEnd_city());
                startActivity(new Intent(mActivity, IssueNewOrderActivity.class)
                        .putExtra("order_type", "1")
                        .putExtra("place_order_type", "1")
                        .putExtra("networkCity", data.getEnd_city())
                        .putExtra("companyInfo", companyInfo));
            }
        });
    }

    @Override
    public void onFailure(int requestCode) {

    }
}